define(["jquery", "bootstrap", "backend", "table", "form"], function ($, undefined, Backend, Table, Form) {
  var Controller = {
    index: function () {
      var resData = null; // 查询季度统计 服务器返回数据

      document.addEventListener("DOMContentLoaded", function () {
        console.log("!!!!!!!!!!!!!!");
        // 确保 DOM 已经完全加载
        var departmentSelect = document.getElementById("department-select");
        if (departmentSelect.options.length > 0) {
          console.log("科室选项已加载完成");
          // 在这里可以执行其他操作
        } else {
          console.log("科室选项尚未加载完成");
        }
      });

      console.log("jQuery version:", $().jquery); // 检查 jQuery 版本

      // 显示当前日期组件
      require(["bootstrap-datetimepicker"], function () {
        var options = {
          format: "YYYY-MM-DD HH:mm:ss",
          icons: {
            time: "fa fa-clock-o",
            date: "fa fa-calendar",
            up: "fa fa-chevron-up",
            down: "fa fa-chevron-down",
            previous: "fa fa-chevron-left",
            next: "fa fa-chevron-right",
            today: "fa fa-history",
            clear: "fa fa-trash",
            close: "fa fa-remove",
          },
          showTodayButton: true,
          showClose: true,
        };
        var form = $("#date-form");
        $(".datetimepicker", form).parent().css("position", "relative");
        $(".datetimepicker", form)
          .datetimepicker(options)
          .on("dp.change", function (e) {
            $(this, document).trigger("changed");
          });
      });

      // 选择科室 事件
      $("#department-select").on("change", function () {
        var selectedValue = $(this).val();
        console.log("Selected Department ID:", selectedValue);
      });

      //确定 按钮 #query-btn 点击事件 ajax 请求数据
      $("#query-btn").on("click", function (event) {
        event.preventDefault(); // 阻止表单的默认提交行为
        var department = $("#department-select").val();
        var quarter_select = $("#quarter-select").val();
        console.log("department:", department);
        console.log("quarter-select:", quarter_select);

        // 判断start_time是否为空, 为空则提示
        if (quarter_select == "") {
          layer.alert("请选择季度", { icon: 2, time: 3000 });
          return false;
        }

        // ajax 请求数据
        $.ajax({
          url: "fssy/nursecount/getNurseAllCountData",
          type: "POST",
          data: {
            department, // 科室
            quarter_select, // 季度
          },
          success: function (res) {
            console.log("success = ", res);
            res.msg = "OK" ? layer.alert("查询成功", { icon: 1 }) : layer.alert("查询失败", { icon: 2 });
            resData = res.data;
            res = res.data;
            priv_ange_title = res.PriviousData.jobTitleQuarterArr.range_title;
            ckd_ange_title = res.CheckedData.jobTitleQuarterArr.range_title;

            $(".jt-priv-Q, .eb-priv-Q, .nz-priv-Q").text(priv_ange_title);
            $(".jt-ckd-Q,.eb-ckd-Q, .nz-ckd-Q, .ckd-Q").text(ckd_ange_title);
            $(
              ".jt-priv-Q, .jt-ckd-Q, .jt-Num-ckd, .jt-Num-priv, .eb-priv-Q, .eb-ckd-Q, .eb-Num-ckd, .eb-Num-priv, .nz-priv-Q, .nz-ckd-Q, .nz-Num-ckd, .nz-Num-priv"
            ).css("font-weight", "900");

            $("#zc01rs-prev-Num").text(res.PriviousData.jobTitleQuarterArr["护士"]);
            $("#zc02rs-prev-Num").text(res.PriviousData.jobTitleQuarterArr["护师"]);
            $("#zc03rs-prev-Num").text(res.PriviousData.jobTitleQuarterArr["主管护师"]);
            $("#zc04rs-prev-Num").text(res.PriviousData.jobTitleQuarterArr["副主任护师"]);
            $("#zc05rs-prev-Num").text(res.PriviousData.jobTitleQuarterArr["主任护师"]);

            $("#zc01rs-ckd-Num").text(res.CheckedData.jobTitleQuarterArr["护士"]);
            $("#zc02rs-ckd-Num").text(res.CheckedData.jobTitleQuarterArr["护师"]);
            $("#zc03rs-ckd-Num").text(res.CheckedData.jobTitleQuarterArr["主管护师"]);
            $("#zc04rs-ckd-Num").text(res.CheckedData.jobTitleQuarterArr["副主任护师"]);
            $("#zc05rs-ckd-Num").text(res.CheckedData.jobTitleQuarterArr["主任护师"]);

            $("#xl01rs-prev-Num").text(res.PriviousData.educationBakQuarterArr["中专"]);
            $("#xl02rs-prev-Num").text(res.PriviousData.educationBakQuarterArr["大专"]);
            $("#xl03rs-prev-Num").text(res.PriviousData.educationBakQuarterArr["本科"]);
            $("#xl04rs-prev-Num").text(res.PriviousData.educationBakQuarterArr["硕士"]);
            $("#xl05rs-prev-Num").text(res.PriviousData.educationBakQuarterArr["博士"]);

            $("#xl01rs-ckd-Num").text(res.CheckedData.educationBakQuarterArr["中专"]);
            $("#xl02rs-ckd-Num").text(res.CheckedData.educationBakQuarterArr["大专"]);
            $("#xl03rs-ckd-Num").text(res.CheckedData.educationBakQuarterArr["本科"]);
            $("#xl04rs-ckd-Num").text(res.CheckedData.educationBakQuarterArr["硕士"]);
            $("#xl05rs-ckd-Num").text(res.CheckedData.educationBakQuarterArr["博士"]);

            $("#nz01rs-prev-Num").text(res.PriviousData.yearPayQuarterArr["<1年资人数"]);
            $("#nz02rs-prev-Num").text(res.PriviousData.yearPayQuarterArr["1≤y<2年资人数"]);
            $("#nz03rs-prev-Num").text(res.PriviousData.yearPayQuarterArr["2≤y<5年资人数"]);
            $("#nz04rs-prev-Num").text(res.PriviousData.yearPayQuarterArr["5≤y<10年资人数"]);
            $("#nz05rs-prev-Num").text(res.PriviousData.yearPayQuarterArr["10≥y<20年资人数"]);
            $("#nz06rs-prev-Num").text(res.PriviousData.yearPayQuarterArr["≥20年资人数"]);

            $("#nz01rs-ckd-Num").text(res.CheckedData.yearPayQuarterArr["<1年资人数"]);
            $("#nz02rs-ckd-Num").text(res.CheckedData.yearPayQuarterArr["1≤y<2年资人数"]);
            $("#nz03rs-ckd-Num").text(res.CheckedData.yearPayQuarterArr["2≤y<5年资人数"]);
            $("#nz04rs-ckd-Num").text(res.CheckedData.yearPayQuarterArr["5≤y<10年资人数"]);
            $("#nz05rs-ckd-Num").text(res.CheckedData.yearPayQuarterArr["10≥y<20年资人数"]);
            $("#nz06rs-ckd-Num").text(res.CheckedData.yearPayQuarterArr["≥20年资人数"]);

            var totalSum = 0;
            $(".jt-Num-ckd").each(function () {
              var num = parseFloat($(this).text());
              if (!isNaN(num)) {
                totalSum += num;
              }
            });
            $("#jt-Sum-ckd").text(totalSum);

            totalSum = 0;
            $(".eb-Num-ckd").each(function () {
              var num = parseFloat($(this).text());
              if (!isNaN(num)) {
                totalSum += num;
              }
            });
            $("#eb-Sum-ckd").text(totalSum);

            totalSum = 0;
            $(".nz-Num-ckd").each(function () {
              var num = parseFloat($(this).text());
              if (!isNaN(num)) {
                totalSum += num;
              }
            });
            $("#nz-Sum-ckd").text(totalSum);
          },
          error: function (jqXHR, textStatus, errorThrown) {
            console.log(textStatus, errorThrown);
          },
        });
      });

      // 导出excel按钮 #output-excel-btn 点击事件 ajax 请求数据
      $("#output-excel-btn").on("click", function (event) {
        event.preventDefault(); // 阻止表单的默认提交行为

        if (resData == null) {
          layer.alert("请先查询数据", { icon: 2, time: 3000 });
          return false;
        }

        console.log("resData = ", resData);

        // 拼接请求的 URL 并触发文件下载
        const form = $("<form>").attr("style", "display:none").attr("method", "POST").attr("action", "nursecount/outputExcel");
        // 将表单添加到页面并提交
        $("body").append(form);
        form.submit();
        form.remove();

        // $.ajax({
        //   url: "fssy/nursecount/outputExcel",
        //   type: "POST",
        //   data: { resData: JSON.stringify(resData) },
        //   success: function (data) {
        //     console.log("success = ", data);
        //     if (data.code === 1) {
        //       alert("文件已成功导出");
        //     } else {
        //       alert("导出失败: " + data.msg);
        //     }
        //   },
        //   error: function (jqXHR, textStatus, errorThrown) {
        //     console.log(textStatus, errorThrown);
        //   },
        // });
      });

      Controller.api.bindevent();
    },

    add: function () {
      Controller.api.bindevent();
    },

    edit: function () {
      Controller.api.bindevent();
    },

    api: {
      bindevent: function () {
        Form.api.bindevent($("form[role=form]"));
      },
    },
  };

  return Controller;
});
